package UserFriend;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import Logcheck.CheckConstant;
import serve.Constant;

import java.net.DatagramPacket;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class Friend_check
{
    public static JSONObject friend_search(DatagramPacket datagramPacket)
    {
        JSONObject jsonObject = JSONObject.parseObject(new String(datagramPacket.getData()));
        String targetName = jsonObject.getString("targetName");

        JSONObject send_json = new JSONObject();
        int code = 0;
        String message = "";

        boolean is_user_exist;
        try
        {
            String sql = "select * from %s where user_name=\"%s\"";
            sql = String.format(sql,CheckConstant.usertable,targetName);
            Statement stmt = Constant.chatdbConnector.getChatdb_connection().createStatement();
            ResultSet res = stmt.executeQuery(sql);
            is_user_exist = res.next();
            if(is_user_exist)
            {
                //send_json.put("title",FriendConstant.search_succeed);
                code = CheckConstant.True;
                message = FriendConstant.search_succeed;
                send_json.put("username",res.getString(1));
                send_json.put("sign",res.getString(7));
                send_json.put("allowStrangerChat",res.getString(6));
            }
            else
            {
                //send_json.put("title",FriendConstant.search_failed);
                send_json.put("title",FriendConstant.search_failed);
                code = CheckConstant.False;
                message = "用户名不存在";
            }
        }catch (SQLException e)
        {
            e.printStackTrace();
        }

        send_json.put("code",code);
        send_json.put("message",message);

        return send_json;
    }

    public static void friend_add(DatagramPacket datagramPacket)
    {
        JSONObject jsonObject = JSONObject.parseObject(new String(datagramPacket.getData()));
        String username = jsonObject.getString("username");
        String targetUsername = jsonObject.getString("targetUsername");

        try
        {
            String sql = "update %s set user_friend_count=user_friend_count+1 where user_name=\"%s\" or user_name=\"%s\"";
            sql = String.format(sql,CheckConstant.usertable,username,targetUsername);

            PreparedStatement stat = Constant.chatdbConnector.getChatdb_connection().prepareStatement(sql);
            stat.executeUpdate();
            stat.close();
        }catch (SQLException e)
        {
            e.printStackTrace();
        }

        int con = 0;

        try
        {
            String sql = "select * from %s where user_name=\"%s\"";
            sql = String.format(sql,FriendConstant.friend_table,username);

            Statement stmt = Constant.chatdbConnector.getChatdb_connection().createStatement();
            ResultSet res = stmt.executeQuery(sql);
            res.next();

            //System.out.print(res.getString(2));

            for(int i=2;i<=6;i++)
            {
                String string = res.getString(i);
                if(string.equals("empty;"))
                {
                    con = i;
                    break;
                }
            }

            String sql_1 = "update %s set %s=\"%s\" where user_name=\"%s\"";
            sql_1 = String.format(sql_1,FriendConstant.friend_table,"friend_"+(con-1),targetUsername,username);
            PreparedStatement stat = Constant.chatdbConnector.getChatdb_connection().prepareStatement(sql_1);
            stat.executeUpdate();
            stat.close();

        }catch (SQLException e)
        {
            e.printStackTrace();
        }

        try
        {
            String sql = "select * from %s where user_name=\"%s\"";
            sql = String.format(sql,FriendConstant.friend_table,targetUsername);

            Statement stmt = Constant.chatdbConnector.getChatdb_connection().createStatement();
            ResultSet res = stmt.executeQuery(sql);
            res.next();

            for(int i=2;i<=6;i++)
            {
                String string = res.getString(i);
                if(string.equals("empty;"))
                {
                    con = i;
                    break;
                }
            }

            String sql_1 = "update %s set %s=\"%s\" where user_name=\"%s\"";
            sql_1 = String.format(sql_1,FriendConstant.friend_table,"friend_"+(con-1),username,targetUsername);
            PreparedStatement stat = Constant.chatdbConnector.getChatdb_connection().prepareStatement(sql_1);
            stat.executeUpdate();
            stat.close();
        }catch (SQLException e)
        {
            e.printStackTrace();
        }
    }

    public static JSONObject get_friend_list(DatagramPacket datagramPacket)
    {
        JSONObject jsonObject = JSONObject.parseObject(new String(datagramPacket.getData()));
        String username = jsonObject.getString("username");

        JSONObject send_json = new JSONObject();
        JSONArray send_json_array = new JSONArray();

        try
        {
            String sql = "select * from %s where user_name=\"%s\"";
            sql = String.format(sql,FriendConstant.friend_table,username);

            Statement stmt = Constant.chatdbConnector.getChatdb_connection().createStatement();
            ResultSet res = stmt.executeQuery(sql);

            if(res.next())
            {
                for (int i=2;i<6;i++)
                {
                    String name = res.getString(i);
                    if(!name.equals("empty;"))
                    {
                        JSONObject temp = new JSONObject();
                        temp.put("username",name);
                        send_json_array.add(temp);
                    }
                }
            }
            stmt.close();
        }catch (SQLException e)
        {
            e.printStackTrace();
        }

        if(!send_json_array.isEmpty())
        {
            send_json.put("code",CheckConstant.True);
        }
        else
        {
            send_json.put("code",CheckConstant.False);
        }
        send_json.put("data",send_json_array);

        return send_json;
    }
}
